package com.fy.service.server.controller;

import com.fy.common.constant.Constants;
import com.fy.common.model.im.MessageModel;
import com.fy.service.service.MessageService;
import com.xiaoju.uemc.tinyid.client.utils.TinyId;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

/**
 * Description： 测试作用
 */
@ApiIgnore
@Controller
@RequestMapping(value = "/hbase")
public class HbaseController {

    @Autowired
    private MessageService messageService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private Connection hbaseConnection;


    @ResponseBody
    @RequestMapping(value = "/testInsertMessage", produces = {"application/json;charset=UTF-8"})
    public String TestSend() {

        try {
            MessageModel dto = new MessageModel();
            dto.setCmd("100001");
            dto.setMessageId(TinyId.nextId(Constants.TINYID_MESSAGE_ID));
            dto.setFrom(1l);
            dto.setTo(5l);
            dto.setTenantId("pig");
            dto.setContent("测试数据");
            dto.setSendTime(new Date().getTime());
            dto.setContentType(1);
            dto.setSize(100);
            messageService.saveSingleMessage(dto);
            return "成功";
        } catch (Exception e) {
            logger.error("hbase写入数据错误",e);
            return "失败";
        }
    }



    @ResponseBody
    @RequestMapping(value = "/createTable", produces = {"application/json;charset=UTF-8"})
    public String createTable() {

        try {
            Admin admin = hbaseConnection.getAdmin();
            //构建一个Test_teacher_info表
            TableDescriptorBuilder message = TableDescriptorBuilder.newBuilder(TableName.valueOf("message:message"));
            //ColumnFamilyDescriptorBuilder.newBuilder(ColumnFamilyDescriptor);
            //ColumnFamilyDescriptor family=
            //ColumnFamilyDescriptor c=new C
            //创建列族  1
            ColumnFamilyDescriptor of = ColumnFamilyDescriptorBuilder.of("message");
            message.setColumnFamily(of);
            //构建
            TableDescriptor build = message.build();
            //创建表
            admin.createTable(build);
            // 关闭连接
            admin.close();
            hbaseConnection.close();
            return "成功";
        } catch (Exception e) {
            logger.error("hbase写入数据错误",e);
            return "失败";
        }
    }






}